//
//  PaperMainHomeVC.m
//  Doctor
//
//  Created by 阳光 on 16/9/6.
//  Copyright © 2016年 阳光. All rights reserved.
//

#import "PaperMainHomeVC.h"
#import "PaperListCell.h"
#import "PaperDetailVC.h"

#import "AddNewPaperVC.h"
#import "PaperListModel.h"

static NSString *paperListCellIdentifier=@"PaperListCellIdentifier";

@interface PaperMainHomeVC ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,strong)UITableView *paperMainHomeTableView;

@property(nonatomic,strong)NSMutableArray *paperListArr;

@end

@implementation PaperMainHomeVC

-(void)viewDidLoad
{
    [super viewDidLoad];

    self.title = NSLocalizedString(@"My Articles", nil);//@"论文管理";
    
    _paperListArr=[NSMutableArray array];
    
    [self initTableView];
    
    [self initNavBarBtnItem];
    
    [self setMJRefreshTableView];
}

-(void)setMJRefreshTableView
{
    _paperMainHomeTableView.mj_header=[MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
    [_paperMainHomeTableView.mj_header beginRefreshing];
}


-(void)loadNewData
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),^{
        [self getDoctorPublicList];
        dispatch_async(dispatch_get_main_queue(), ^{
            //update UI
        });
    });
}


-(void)getDoctorPublicList
{
    [_paperListArr removeAllObjects];
    
    NSDictionary *dic=@{
                        @"doctor_id":[Util getUserDefaultObjectForKey:@"id"],
                        @"count":@"20",
                        @"page":@"1"
                        };
    
     __weak typeof(self) weakSelf = self;
    [QBRequest DoctorPublicListWithDic:dic Success:^(id res)
     {
//         NSLog(@"201923---%@",res);
         
         if ([res[@"info"]count]>0)
         {
             weakSelf.paperListArr=[PaperListModel mj_objectArrayWithKeyValuesArray:res[@"info"]];
         }
         else
         {
             [self.view makeToast:@"暂无名医发布数据" duration:1.0 position:CSToastPositionCenter];
         }
         
         [weakSelf.paperMainHomeTableView reloadData];
         [weakSelf.paperMainHomeTableView.mj_header endRefreshing];
         
    } failure:^(NSString *error) {
        
         [weakSelf.paperMainHomeTableView.mj_header endRefreshing];
    }];
}


-(void)initNavBarBtnItem
{
    // 添加帖子发布
    self.navigationItem.rightBarButtonItem=[UIBarButtonItem initWithNormalImage:@"add" target:self action:@selector(addItemEvent)];
}

-(void)addItemEvent
{
    AddNewPaperVC *addNewPaperVC=[AddNewPaperVC new];
    [self.navigationController pushViewController:addNewPaperVC animated:NO];
    
}

-(void)initTableView
{
    // _paperMainHomeTableView
    _paperMainHomeTableView=[[UITableView alloc]initWithFrame:CGRectMake(0,1,APPW,APPH-49) style:UITableViewStylePlain];
    _paperMainHomeTableView.delegate=self;
    _paperMainHomeTableView.dataSource=self;
    _paperMainHomeTableView.hidden=NO;
    _paperMainHomeTableView.backgroundColor=MAIN_BACK_COLOR;
    _paperMainHomeTableView.showsVerticalScrollIndicator=NO;
    _paperMainHomeTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_paperMainHomeTableView];
    [_paperMainHomeTableView registerClass:[PaperListCell class] forCellReuseIdentifier:paperListCellIdentifier];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.paperListArr.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    PaperListCell *cell=[tableView dequeueReusableCellWithIdentifier:paperListCellIdentifier];
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    cell.paperListModel=self.paperListArr[indexPath.row];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return kmargin*2;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *footView=[UIView new];
    return footView;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    
    // 跳到详情界面
    PaperDetailVC *paperDetailVC=[PaperDetailVC new];
    PaperListModel *paperListModel=self.paperListArr[indexPath.row];
    paperDetailVC.paperID=paperListModel.ID;
    [self.navigationController pushViewController:paperDetailVC animated:NO];
}


@end
